<script setup lang="ts">
import { getHomeCategoryAPI } from '@/services/home'
import { ref, reactive } from 'vue'

const state = reactive({
  filter: {
    title: '',
  },
  CurrentPage: 1,
  PageSize: 10,
})
// 获取任务列表
const categoryList = ref<any>([])
const getHomeCategoryData = async () => {
  const res = await getHomeCategoryAPI(state)
  if (res.success) {
    if (!res.data.list.length) {
      state.CurrentPage = 1
      uni.showToast({
        title: '没有更多数据了',
        icon: 'none',
      })
    }
    loading.value = false
    categoryList.value.push(...res.data.list)
  }
}
const godetail = (id: number) => {
  uni.navigateTo({
    url: '/pagesMember/quest-detail/quest?id=' + id,
  })
}
const loading = ref(false)
const loadMore = async () => {
  if (!loading.value) {
    loading.value = true
    state.CurrentPage++
    await getHomeCategoryData()
  }
}
const onsearch = () => {
  categoryList.value = []
  state.CurrentPage = 1
  getHomeCategoryData()
}
</script>

<template>
  <view class="navbar">
    <view class="search">
      <input
        type="text"
        v-model="state.filter.title"
        placeholder="关键词"
        style="font-size: 26rpx; width: 100%; color: #000"
      />
    </view>
    <view class="searchrext" @click="onsearch"> 立即搜索 </view>
  </view>
  <view class="nosearch" v-if="!categoryList.length">
    <img src="@/static/images/nosearch.png" alt="" />
    <view class="">没有找到</view>
  </view>
  <view class="viewport" v-else>
    <!-- 滚动容器 -->
    <scroll-view enable-back-to-top class="scroll-view" @scrolltolower="loadMore" scroll-y>
      <view class="task" v-for="item in categoryList" :key="item.id" @click="godetail(item.id)">
        <view class="task-title">
          <img src="@/static/images/redbag.png" alt="" />
          <view class="">{{ item.title }}</view>
        </view>
        <view class="task-price">
          <view class=""><text style="color: #999">任务编号：</text>{{ item.id }}</view>
          <view class="price">￥{{ item.price }}</view>
        </view>
        <view
          class=""
          style="
            margin-top: 20rpx;
            display: flex;
            align-items: center;
            justify-content: space-between;
          "
        >
          <view class=""
            ><text style="color: #999">截止时间：</text
            >{{ item.endDate.substring(0, 16).replace(' ', ' ') }}</view
          >
          <view class=""><text style="color: #999">招募人数：</text>{{ item.receiveMax }}</view>
        </view>
      </view>
      <view v-if="loading" class="loading">加载中...</view>
    </scroll-view>
  </view>
</template>

<style lang="scss">
.loading {
  text-align: center;
  margin-bottom: 10rpx;
  font-size: 24rpx;
}
.nosearch {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 200rpx;
  img {
    height: 200rpx;
  }
  view {
    color: #999;
  }
}
.navbar {
  margin-top: 20rpx;
  background-color: #fff;
  display: flex;
  align-items: center;
  padding: 0 20rpx;
  justify-content: space-between;
  .search {
    flex: 1;
  }
  .searchrext {
    font-size: 24rpx;
    margin-left: 20rpx;
    color: #279fde;
  }
}
.tasklist {
  background-color: #fff;
  padding: 26rpx;
  font-weight: bold;
  font-size: 32rpx;
}

page {
  background-color: #f7f7f7;
  height: 100%;
  overflow: hidden;
}

.task {
  background-color: #fff;
  margin: 20rpx;
  margin-bottom: 30rpx;
  padding: 30rpx;
  border-radius: 10rpx;
  font-size: 24rpx;
  .task-price {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 30rpx;
    padding-bottom: 20rpx;
    border-bottom: 1px solid #eee;
    .price {
      font-size: 40rpx;
      color: red;
      font-weight: bold;
    }
  }
  .task-title {
    display: flex;
    flex-wrap: nowrap;
    margin-bottom: 30rpx;
    view {
      font-size: 28rpx;
      font-weight: bold;
      margin-left: 20rpx;
      color: #4d4d4d;
      word-break: break-all;
    }
    img {
      margin-top: 5rpx;
      height: 30rpx;
      width: 30rpx;
    }
  }
}

.viewport {
  height: calc(100% - 100rpx);
  display: flex;
  flex-direction: column;
}

.scroll-view {
  flex: 1;
  overflow: hidden;
}

.button {
  margin: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 140rpx;
  height: 50rpx;
  font-size: 28rpx;
  border-radius: 72rpx;
  color: #fff;
  background-color: #1e9fff;
}

.search {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex: 0.9;
  padding: 0 10rpx 0 26rpx;
  height: 64rpx;
  margin: 16rpx 0;
  color: #999;
  font-weight: 500;
  font-size: 28rpx;
  border-radius: 32rpx;
  background-color: #f2f2f2;
}

.icon-search {
  &::before {
    margin-right: 10rpx;
  }
}
</style>
